package com.blb.web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.blb.service.BaseService;
import com.blb.service.impl.BaseServiceImpl;
import com.blb.utils.TableEntity;
import com.blb.utils.UUIDUtils;
import com.blb.web.base.BaseServlet;
import com.mysql.jdbc.StringUtils;

@WebServlet("/revenue/*")
public class RevenueServlet extends BaseServlet {

	private BaseService service = new BaseServiceImpl();
	
	private static final long serialVersionUID = 7060561389976928528L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.forward(request, response, this.getClass());
	}
	
	/**
	 * 收入列表界面
	 * @param request
	 * @param response
	 */
	public void revenueList(HttpServletRequest request, HttpServletResponse response) {
		try {
			super.setPageSize(request.getParameter("limit"));
			super.setPageNo(request.getParameter("page"));
			
			String type = request.getParameter("type");
			String createDate = request.getParameter("month");
			
			//组装参数
			List<Object> params = new ArrayList<Object>();
			params.add(((Map)request.getSession().getAttribute("user")).get("username").toString());
			
			StringBuffer sql = new StringBuffer("SELECT r.*, u.`username` FROM `t_revenue` r LEFT JOIN `t_user` u ON r.`user_id` = u.`id` WHERE u.`username` = ? ");
			if (!StringUtils.isNullOrEmpty(type)) {
				sql.append(" and type = ?");
				params.add(type);
			}
			if (!StringUtils.isNullOrEmpty(createDate)) {
				String[] date = createDate.split(" - ");
				sql.append(" AND create_date >= ? AND create_date <= ?");
				params.add(date[0]);
				params.add(date[1]);
			}
		
			//查询总条数
			List<Map<String, Object>> list2 = service.get(sql.toString(), params.toArray(new Object[] {}));
			
			params.add(super.getPageNo());
			params.add(super.getPageSize());
			
			//查询分页数据
			List<Map<String, Object>> list = service.get(sql.toString() + " order by create_date desc limit ?, ?", params.toArray(new Object[] {}));
			
			
			//封装layui 表格需要的实体
			TableEntity tableEntity = new TableEntity();
			tableEntity.setData(list);
			tableEntity.setCount(list2.size());
			//返回分页json数据
			JSONObject.DEFFAULT_DATE_FORMAT="yyyy-MM-dd";
			super.responseJson(JSONObject.toJSONString(tableEntity, SerializerFeature.WriteDateUseDateFormat));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 新增一笔收入
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void addRevenue(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String type = request.getParameter("type");
		String money = request.getParameter("money");
		String remark = request.getParameter("remark");
		String createDate = request.getParameter("create_date");
		
		String sql = "INSERT INTO `familycashbook`.`t_revenue` (`id`,`money`,`type`,`create_date`,`remark`,`user_id`)VALUES(?,?,?,?,?,?)";
		try {
			service.execute(sql, UUIDUtils.getId(), money, type, createDate, remark, request.getSession().getAttribute("user_id").toString());
			super.responseJson("{\"result\" : true}");
		} catch (SQLException e) {
			e.printStackTrace();
			super.responseJson("{\"result\" : false, \"msg\" : \"系统异常\"：" + e.getMessage() + "}");
		}
	}
}
